use chrono::{NaiveDateTime, Timelike, Local, Datelike};
use crate::service::model::chat_model::{Message, GroupMessage};

pub fn group_item_by_time(source: Vec<Message>) -> Vec<GroupMessage> {
    fn format_group_time(mut list: Vec<GroupMessage>)-> Vec<GroupMessage> {
        let mut new_list: Vec<GroupMessage> = Vec::new();
        let len = list.len();
        for _ in 0..len {
            let mut item = list.remove(0);
            item.time=get_time_format(&item.list.last().unwrap().create_time);
            new_list.push(item);
        }
        new_list
    }

    let mut list: Vec<GroupMessage> = Vec::new();
    let now = Local::now().naive_local();
    for message in source {
        let time = NaiveDateTime::parse_from_str(&message.create_time, "%Y-%m-%d %H:%M:%S").unwrap();
        if list.len() == 0 {
            let mut group = GroupMessage::new(time);
            group.list.push(message);
            list.push(group);

            continue;
        }

        let now_diff = diff_time_day(now, time);

        let last_group_index = list.len() - 1;


        if now_diff < 1 {
            let date_time = NaiveDateTime::parse_from_str(&list[last_group_index].time, "%Y-%m-%d %H:%M:%S").unwrap();
            if date_time.year() == time.year() && date_time.month() == time.month() && date_time.day() == time.day() {
                let last_value_index = &list[last_group_index].list.len() - 1;
                let last_value_time = NaiveDateTime::parse_from_str(&list[last_group_index].list[last_value_index].create_time, "%Y-%m-%d %H:%M:%S").unwrap();
                if diff_time_minute(time, last_value_time) < 4 {
                    &list[last_group_index].list.push(message);
                    continue;
                }
            }
        }else  if now_diff ==1 {
            let date_time = NaiveDateTime::parse_from_str(&list[last_group_index].time, "%Y-%m-%d %H:%M:%S").unwrap();
            if date_time.year() == time.year() && date_time.month() == time.month() && date_time.day() == time.day() {
                let last_value_index = &list[last_group_index].list.len() - 1;
                let last_value_time = NaiveDateTime::parse_from_str(&list[last_group_index].list[last_value_index].create_time, "%Y-%m-%d %H:%M:%S").unwrap();
                if diff_time_minute(time, last_value_time) < 4 {
                    &list[last_group_index].list.push(message);
                    continue;
                }
            }
        }else {
            let date_time = NaiveDateTime::parse_from_str(&list[last_group_index].time, "%Y-%m-%d %H:%M:%S").unwrap();
            if date_time.year() == time.year() && date_time.month() == time.month() && date_time.day() == time.day() {
                &list[last_group_index].list.push(message);
                continue;
            }
        }

        let mut group = GroupMessage::new(time);
        group.list.push(message);
        list.push(group);
    }


    format_group_time(list)
}




pub fn get_time_format(time: &str) -> String {
    let time1 = NaiveDateTime::parse_from_str(time, "%Y-%m-%d %H:%M:%S").unwrap();
    get_time_format_by_time(time1)
}

pub fn get_time_format_by_time(time: NaiveDateTime) -> String {
    let now = Local::now().naive_local();

    let now_diff = diff_time_day(now, time);
    if now_diff<1 {
        if time.hour() < 12 {
            return format!("上午{0}",time.format("%H:%M"));
        } else {
            return format!("下午{0}",time.format("%H:%M"));
        }

    }else if now_diff ==1 {
        if time.hour() < 12 {
            return format!("昨天 上午{0}",time.format("%H:%M"));
        } else {
            return format!("昨天 下午{0}", time.format("%H:%M"));
        }
    }
    time.format("%Y-%m-%d %H:%M").to_string()
}


//计算天差
pub fn diff_time_day(now: NaiveDateTime, time: NaiveDateTime) -> i64 {
    let now_time=now.time();
    let time_time=time.time();
    let diff_timestamp = now.timestamp() - time.timestamp();

    let mut day=diff_timestamp / 3600 / 24;
    if now_time.hour()<time_time.hour(){
        return day+1;
    }else if now_time.hour()>time_time.hour(){
        return day;
    }
    if now_time.minute()<time_time.minute(){
        return day+1;
    }else if now_time.minute()>time_time.minute(){
        return day;
    }
    if now_time.second()<time_time.second(){
        return day+1;
    }else if now_time.second()>time_time.second(){
        return day;
    }
    day
}

//计算分差
pub fn diff_time_minute(tim1: NaiveDateTime, time2: NaiveDateTime) -> i64 {
    let diff_timestamp = tim1.timestamp() - time2.timestamp();

    diff_timestamp / 60
}
